<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author=>冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace api\modules\admin\controllers\system;

use api\modules\admin\AuthController;
use api\utils\libs\CommonAction;
use common\cache\ConfigCache;
use common\models\system\Config;
use common\services\system\ConfigService;

class ConfigController extends AuthController
{
    use CommonAction;

    protected string $model = Config::class;
    protected bool $validate = true;
    protected array $noAction = ['status'];

    /**
     * 根据键获取参数值
     * @return array
     */
    public function actionGet(): array
    {
        $key = trim($this->request->get('key',''));
        return $this->success('',['value'=>ConfigService::getConfigValue($key)]);
    }

    /**
     * 清除缓存
     * @return array
     */
    public function actionCache(): array
    {
        ConfigCache::clear(true);
        return $this->success();
    }

    protected function deleteBefore(Config $model): string
    {
        if ($model->is_sys == '1') return '系统配置，无法删除';
        return '';
    }

    protected function exportBefore(array $list): array
    {
        $row = [
            'id' => '编码',
            'name' => '配置名称',
            'type' => '配置标识',
            'value' => '配置值',
            'update_time' => '更新时间'
        ];
        $bold = ['A1:F1' => true];
        $width = ['a' => 10, 'b' => 20, 'c' => 20, 'd' => 40, 'e' => 10];
        $size = ['a1:A6' => 18, 'b2:c5' => 14];
        $wrap = ['e' => true];
        $background = ['b' => 'FF0000'];
        $color = ['a1' => 'FF0000'];
        return ['list' => $list, 'row' => $row, 'fileName' => '配置列表', 'bold' => $bold, 'width' => $width, 'size' => $size, 'wrap' => $wrap, 'background' => $background, 'color' => $color];
    }
}
